﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class LoaiBenhDAL
    {
        PhongMachTuDBDataContext db;
        public List<LOAIBENH> LayDSLoaiBenh()
        {
            db = new PhongMachTuDBDataContext();
            var q = from s in db.LOAIBENHs
                    select s;
            return q.ToList();
        }
        public IQueryable timKiemLoaiBenh(string ma,string ten)
        {
            var q = from s in db.LOAIBENHs
                    where s.MaLoaiBenh==ma || s.TenLoaiBenh==ten
                    select s;
            return q;
        }
        public bool ThemLoaiBenh(LOAIBENH s)
        {

            try
            {
                db = new PhongMachTuDBDataContext();
                LOAIBENH r = new LOAIBENH();
                r.TenLoaiBenh = s.TenLoaiBenh;
                r.MaLoaiBenh = s.MaLoaiBenh;
                db.LOAIBENHs.InsertOnSubmit(r);
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool suaLoaiBenh(LOAIBENH s)
        {

            try
            {
                db = new PhongMachTuDBDataContext();
                LOAIBENH r = db.LOAIBENHs.Single(id => id.MaLoaiBenh == s.MaLoaiBenh);
                r.TenLoaiBenh = s.TenLoaiBenh;
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool xoaLoaiBenh(string MaLoaiBenh)
        {
            try
            {
                db = new PhongMachTuDBDataContext();
                var delete = from d in db.LOAIBENHs
                             where d.MaLoaiBenh == MaLoaiBenh
                             select d;
                if (delete.Count() > 0)
                {
                    db.LOAIBENHs.DeleteOnSubmit(delete.FirstOrDefault());
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
